using System;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.Users;
using Volo.Abp.MultiTenancy;
using JetBrains.Annotations;

namespace CKY.AgentPlatform.Users
{
    /// <summary>
    /// 平台用户实体
    /// </summary>
    public class PlatformUser : FullAuditedAggregateRoot<Guid>, IUser, IMultiTenant
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public virtual string UserName { get; set; }

        /// <summary>
        /// 邮箱
        /// </summary>
        public virtual string Email { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public virtual string Name { get; set; }

        /// <summary>
        /// 姓氏
        /// </summary>
        public virtual string Surname { get; set; }

        /// <summary>
        /// 电话号码
        /// </summary>
        public virtual string PhoneNumber { get; set; }

        /// <summary>
        /// 是否邮箱已确认
        /// </summary>
        public virtual bool EmailConfirmed { get; set; }

        /// <summary>
        /// 是否电话已确认
        /// </summary>
        public virtual bool PhoneNumberConfirmed { get; set; }

        /// <summary>
        /// 是否启用双因子认证
        /// </summary>
        public virtual bool TwoFactorEnabled { get; set; }

        /// <summary>
        /// 锁定结束时间
        /// </summary>
        public virtual DateTime? LockoutEnd { get; set; }

        /// <summary>
        /// 是否已锁定
        /// </summary>
        public virtual bool IsLockedOut => LockoutEnd.HasValue && LockoutEnd.Value > DateTime.UtcNow;

        /// <summary>
        /// 访问失败次数
        /// </summary>
        public virtual int AccessFailedCount { get; set; }

        /// <summary>
        /// 头像URL
        /// </summary>
        public virtual string AvatarUrl { get; set; }

        /// <summary>
        /// 部门
        /// </summary>
        public virtual string Department { get; set; }

        /// <summary>
        /// 职位
        /// </summary>
        public virtual string Position { get; set; }

        /// <summary>
        /// 最后登录时间
        /// </summary>
        public virtual DateTime? LastLoginTime { get; set; }

        /// <summary>
        /// 最后登录IP
        /// </summary>
        public virtual string LastLoginIp { get; set; }

        /// <summary>
        /// 是否管理员
        /// </summary>
        public virtual bool IsAdmin { get; set; }

        /// <summary>
        /// 是否活跃
        /// </summary>
        public virtual bool IsActive { get; set; }

        /// <summary>
        /// 密码哈希
        /// </summary>
        public virtual string PasswordHash { get; set; }

        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; set; }

        protected PlatformUser()
        {
            // EF Core required constructor
        }

        public PlatformUser(
            Guid id,
            [NotNull] string userName,
            [NotNull] string email,
            [NotNull] string name,
            [NotNull] string surname)
            : base(id)
        {
            UserName = userName;
            Email = email;
            Name = name;
            Surname = surname;
            EmailConfirmed = false;
            PhoneNumberConfirmed = false;
            TwoFactorEnabled = false;
            AccessFailedCount = 0;
            IsAdmin = false;
            IsActive = true;
        }

        /// <summary>
        /// 记录登录信息
        /// </summary>
        public virtual void RecordLogin(string ipAddress)
        {
            LastLoginTime = DateTime.UtcNow;
            LastLoginIp = ipAddress;
            AccessFailedCount = 0;
        }

        /// <summary>
        /// 增加访问失败次数
        /// </summary>
        public virtual void IncrementAccessFailedCount()
        {
            AccessFailedCount++;
            
            // 如果失败次数达到5次，锁定账户30分钟
            if (AccessFailedCount >= 5)
            {
                LockoutEnd = DateTime.UtcNow.AddMinutes(30);
            }
        }

        /// <summary>
        /// 重置访问失败次数
        /// </summary>
        public virtual void ResetAccessFailedCount()
        {
            AccessFailedCount = 0;
            LockoutEnd = null;
        }

        /// <summary>
        /// 锁定账户
        /// </summary>
        public virtual void LockAccount(TimeSpan? duration = null)
        {
            LockoutEnd = DateTime.UtcNow.Add(duration ?? TimeSpan.FromMinutes(30));
        }

        /// <summary>
        /// 解锁账户
        /// </summary>
        public virtual void UnlockAccount()
        {
            LockoutEnd = null;
            AccessFailedCount = 0;
        }

        /// <summary>
        /// 确认邮箱
        /// </summary>
        public virtual void ConfirmEmail()
        {
            EmailConfirmed = true;
        }

        /// <summary>
        /// 确认电话
        /// </summary>
        public virtual void ConfirmPhoneNumber()
        {
            PhoneNumberConfirmed = true;
        }

        /// <summary>
        /// 启用双因子认证
        /// </summary>
        public virtual void EnableTwoFactor()
        {
            TwoFactorEnabled = true;
        }

        /// <summary>
        /// 禁用双因子认证
        /// </summary>
        public virtual void DisableTwoFactor()
        {
            TwoFactorEnabled = false;
        }

        /// <summary>
        /// 设置管理员状态
        /// </summary>
        public virtual void SetAdminStatus(bool isAdmin)
        {
            IsAdmin = isAdmin;
        }

        /// <summary>
        /// 激活或停用账户
        /// </summary>
        public virtual void SetActiveStatus(bool isActive)
        {
            IsActive = isActive;
        }
    }
}